package form.ui.component.Slider
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import form.events.SliderEvent;
	import form.ui.core.Button;
	import form.utils.MathUtils;

	public class SlideThumb extends Button
	{
		private var slider:Slider;
		
		public function SlideThumb(slider:Slider)
		{
			super();
			
			this.slider = slider;
			
			var up:SliderThumbSkin = new SliderThumbSkin();
			up.name = SliderThumbSkin.NAME_UP;			
			var over:SliderThumbSkin = new SliderThumbSkin();
			over.name = SliderThumbSkin.NAME_OVER;			
			var down:SliderThumbSkin = new SliderThumbSkin();
			down.name = SliderThumbSkin.NAME_DOWN;			
			var disabl:SliderThumbSkin = new SliderThumbSkin();
			disabl.name = SliderThumbSkin.NAME_DISABLE;
			
			this.mouseUp = up;
			this.mouseOver = over;
			this.mouseDown = down;
			this.disabled = disabl;
			
			this.enable();
		}
		/* 
		override protected function onAddedToStage(e:Event):void {
			super.onAddedToStage(e);
		}
		 */
		private function stopDragAction(e:Event):void
		{
			mouseUpAction();
		}
		
		private var currentRect:Rectangle;
		override public function mouseDownAction():void
		{
			currentRect = slider.dragRect;
			this.startDrag(false, currentRect);
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove, false, 0, true);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragAction, false, 0, true);				
		}
		
		private function onMouseMove(e:Event):void
		{
			if(currentRect)
				dispatchEvent(new SliderEvent(SliderEvent.SLIDE_CHANGE, computeValue));
		}
		
		override public function mouseUpAction():void
		{
			this.stopDrag();
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragAction);
			if(currentRect)dispatchEvent(new SliderEvent(SliderEvent.THUMB_RELEASE, computeValue));				
			currentRect = null;
		}
		
		private function get computeValue():Number
		{
			return MathUtils.reangleToPoint(currentRect.x, currentRect.width + currentRect.x, this.x);
		}
		
		override public function get width():Number
		{
			return this.mouseUp ? this.mouseUp.width : super.width;
		}
		
		override public function get height():Number
		{
			return this.mouseUp ? this.mouseUp.height : super.height;
		}
		
	}
}